#include <assert.h>
#include <pthread.h>
#include <stdlib.h>

#include "base/simapi.h"

int g[20]; // for now this test can not work with more than 20 processors
//pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

#define NUM_OF_ITERATIONS		50

void* foo(void* in){
  int tid = Sim_GetMyId();
  int i;
  g[tid]=0;
  //pthread_barrier_wait(&barrier);
  for (i = 0; i < NUM_OF_ITERATIONS; i++){
    g[tid]++;
    //	Sim_Print("Incrementing Counter!\n");
  }
  return NULL;
}


void mainX(int argc, const char** const argv, const char **envp) {
  int i; 
  int expectResult = NUM_OF_ITERATIONS;
  Sim_ParallelFork(&foo, NULL);
  for (i=0;i<Sim_GetNumCpus();i++){
    if(g[i] != expectResult){
      Sim_Print("Expects %0d:  %d\n", i, expectResult);
      Sim_Print("Got %0d:  %d\n\n", i, g[i]);
      assert(0);
    }
  }
  Sim_Print("Correct:  %d\n", g[i-1]);
}
